package com.project.movie.controller.index;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.project.movie.config.utils.ShiroUtils;
import com.project.movie.domain.*;
import com.project.movie.service.MovieCinemaService;
import com.project.movie.service.MovieItemService;
import com.project.movie.service.MovieOrderService;
import com.project.movie.service.UserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.IntStream;

@Controller
@ResponseBody
@RequestMapping("index")
public class IndexController {

    @Autowired
    private MovieOrderService movieOrderService;
    @Autowired
    private MovieItemService movieItemService;
    @Autowired
    private UserService userService;
    @Autowired
    private MovieCinemaService movieCinemaService;

    @GetMapping("getIndexManage")
    public Result getIndexManage() {
        User userInfo = ShiroUtils.getUserInfo();
        JSONObject jsonObject = new JSONObject();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formatted = simpleDateFormat.format(new Date());
        int today = 0;
        int total = 0;
        if (userInfo.getUserType() == 2) {
            QueryWrapper<MovieCinema> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(MovieCinema::getUserId,userInfo.getId());
            List<MovieCinema> cinemaList = movieCinemaService.list(wrapper);
            List<String> list = new ArrayList<>();
            for (MovieCinema movieCinema : cinemaList) {
                list.add(movieCinema.getId());
            }
            QueryWrapper<MovieOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().like(MovieOrder::getCreateTime,formatted).ne(MovieOrder::getState,1)
                    .in(MovieOrder::getCinemaId,list);
            List<MovieOrder> orderList = movieOrderService.list(queryWrapper);
            for (MovieOrder movieOrder : orderList) {
                today += movieOrder.getPrice();
            }

            QueryWrapper<MovieOrder> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().ne(MovieOrder::getState,1).in(MovieOrder::getCinemaId,list);
            List<MovieOrder> orderList1 = movieOrderService.list(queryWrapper1);
            for (MovieOrder movieOrder : orderList1) {
                total += movieOrder.getPrice();
            }
        } else {
            //今日售票
            QueryWrapper<MovieOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().like(MovieOrder::getCreateTime,formatted).ne(MovieOrder::getState,1);
            List<MovieOrder> orderList = movieOrderService.list(queryWrapper);
            for (MovieOrder movieOrder : orderList) {
                today += movieOrder.getPrice();
            }
            //总额
            QueryWrapper<MovieOrder> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().ne(MovieOrder::getState,1);
            List<MovieOrder> orderList1 = movieOrderService.list(queryWrapper1);
            for (MovieOrder movieOrder : orderList1) {
                total += movieOrder.getPrice();
            }
        }
        //用户数
        QueryWrapper<User> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(User::getUserType,1);
        int userNum = userService.count(queryWrapper2);
        int movieNum = movieItemService.count();
        jsonObject.put("today",today);
        jsonObject.put("total",total);
        jsonObject.put("userNum",userNum);
        jsonObject.put("movieNum",movieNum);
        return Result.success(jsonObject);
    }

    @GetMapping("getPolyline")
    public Result getPolyline() {
        User userInfo = ShiroUtils.getUserInfo();
        LocalDate today = LocalDate.now();
        List<String> dates = new ArrayList<>();
        List<Object> nums = new ArrayList<>();
        IntStream.rangeClosed(0, 14)
                .mapToObj(days -> today.minusDays(days))
                .forEach(date -> dates.add(date.format(DateTimeFormatter.ISO_DATE)));
        for (String s : dates) {
            int num = 0;
            if (userInfo.getUserType() == 2) {
                QueryWrapper<MovieCinema> wrapper = new QueryWrapper<>();
                wrapper.lambda().eq(MovieCinema::getUserId,userInfo.getId());
                List<MovieCinema> cinemaList = movieCinemaService.list(wrapper);
                List<String> list = new ArrayList<>();
                for (MovieCinema movieCinema : cinemaList) {
                    list.add(movieCinema.getId());
                }
                QueryWrapper<MovieOrder> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().like(MovieOrder::getCreateTime,s).ne(MovieOrder::getState,1)
                        .in(MovieOrder::getCinemaId,list);
                List<MovieOrder> orderList = movieOrderService.list(queryWrapper);
                for (MovieOrder order : orderList) {
                    num += order.getPrice();
                }
            } else {
                QueryWrapper<MovieOrder> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().like(MovieOrder::getCreateTime,s).ne(MovieOrder::getState,1);
                List<MovieOrder> orderList = movieOrderService.list(queryWrapper);
                for (MovieOrder order : orderList) {
                    num += order.getPrice();
                }
            }
            nums.add(num);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dates",dates);
        jsonObject.put("nums",nums);
        return Result.success(jsonObject);
    }

    @GetMapping("getColumnChart")
    public Result getColumnChart() {
        User userInfo = ShiroUtils.getUserInfo();
        QueryWrapper<MovieItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(MovieItem::getCreateTime);
        List<MovieItem> list = movieItemService.list(queryWrapper);
        List<String> names = new ArrayList<>();
        List<Object> nums = new ArrayList<>();
        for (MovieItem movieItem : list) {
            names.add(movieItem.getName());
            QueryWrapper<MovieOrder> wrapper = new QueryWrapper<>();
            if (userInfo.getUserType() == 2) {
                QueryWrapper<MovieCinema> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.lambda().eq(MovieCinema::getUserId,userInfo.getId());
                List<MovieCinema> cinemaList = movieCinemaService.list(queryWrapper1);
                List<String> list1 = new ArrayList<>();
                for (MovieCinema movieCinema : cinemaList) {
                    list1.add(movieCinema.getId());
                }
                wrapper.lambda().eq(MovieOrder::getItemId,movieItem.getId())
                        .ne(MovieOrder::getState,1).in(MovieOrder::getCinemaId,list1);
            } else {
                wrapper.lambda().eq(MovieOrder::getItemId,movieItem.getId())
                        .ne(MovieOrder::getState,1);
            }
            int price = 0;
            List<MovieOrder> orderList = movieOrderService.list(wrapper);
            for (MovieOrder movieOrder : orderList) {
                price += movieOrder.getPrice();
            }
            nums.add(price);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("names",names);
        jsonObject.put("nums",nums);
        return Result.success(jsonObject);
    }

}
